//
//  TimingSetCell.m
//  GKBHouse
//
//  Created by hcy on 14-1-15.
//
//

#import "TimingSetCell.h"

@implementation TimingSetCell

- (void)dealloc
{
    self.indexPath = nil;
    self.onOffBtn = nil;
    self.timeLabel = nil;
    self.weekLabel = nil;
    self.wiringLabel = nil;
    self.setBtn = nil;
    self.clearBtn = nil;
    
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initCell];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)initCell
{
    CGFloat backTopSpace = 2.0f;
    CGFloat backBorder = 5.0f;
    
    UIImageView *back = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sceneCell.png"]];
    [back setFrame:CGRectMake(backBorder, backTopSpace, kTimingTableWidth - backBorder*2, kTimingSetCellHeight - backTopSpace*2)];
    [self addSubview:back];
    [back release];
    
    UIButton *btnSwitch = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnSwitch setImage:[UIImage imageNamed:@"timingOff.png"] forState:UIControlStateNormal];
    [btnSwitch setImage:[UIImage imageNamed:@"timingOn.png"] forState:UIControlStateSelected];
    [btnSwitch addTarget:self action:@selector(onOffAction) forControlEvents:UIControlEventTouchUpInside];
    CGFloat switchHeight = 46.0f;
    CGFloat switchX = backBorder + 10.0f;
    CGFloat switchY = (kTimingSetCellHeight - switchHeight)/2;
    [btnSwitch setFrame:CGRectMake(switchX, switchY, switchHeight, switchHeight)];
    //    [but addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btnSwitch];
    self.onOffBtn = btnSwitch;
    
    
    //设置与清除
//    CGFloat setTopSpace = backTopSpace + 5.0f;
//    CGFloat mSpace = 5.0f;
    CGFloat setBoder = 10.0f;
    CGFloat setW = 50.0f;
    CGFloat setH = 30.0f;//(kTimingSetCellHeight - setTopSpace*2 - mSpace)/2;
    CGFloat setX = kTimingTableWidth - backBorder - (setBoder + setW)*2;
    CGFloat setY = (kTimingSetCellHeight - setH)/2;
    
    UIButton *setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setBackgroundImage:[UIImage imageNamed:@"blueNormal.png"] forState:UIControlStateNormal];
    [setBtn setBackgroundImage:[UIImage imageNamed:@"blueSelect.png"] forState:UIControlStateHighlighted];
    [setBtn addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
    [setBtn setTitle:NSLocalizedString(@"Timing-set", nil) forState:UIControlStateNormal];
    [setBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [setBtn setFrame:CGRectMake(setX, setY, setW, setH)];
    [self addSubview:setBtn];
    self.setBtn = setBtn;
    
    CGFloat clearX = setX + (setBoder + setW);
    
    UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [clearBtn setBackgroundImage:[UIImage imageNamed:@"blueNormal.png"] forState:UIControlStateNormal];
    [clearBtn setBackgroundImage:[UIImage imageNamed:@"blueSelect.png"] forState:UIControlStateHighlighted];
    [clearBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside];
    [clearBtn setTitle:NSLocalizedString(@"Timing-clear", nil) forState:UIControlStateNormal];
    [clearBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [clearBtn setFrame:CGRectMake(clearX, setY, setW, setH)];
    [self addSubview:clearBtn];
    self.clearBtn = clearBtn;
    
    //时间
    CGFloat labelX = CGRectGetMaxX(btnSwitch.frame) + 10.0f;
    CGFloat labelY = backTopSpace + 2.0f;
    CGFloat labelW = setX - labelX;
    CGFloat labelH = (kTimingSetCellHeight - labelY*2)/3;
    
    UILabel *time = [[UILabel alloc]init];
    [time setFrame:CGRectMake(labelX, labelY, labelW, labelH)];
    [time setText:@""];
    [time setTextColor:[UIColor whiteColor]];
    [self setLabelType:time];
    [self addSubview:time];
    self.timeLabel = time;
    [time release];
    
    //星期
    labelY = labelY + labelH;
    UILabel *week = [[UILabel alloc]init];
    [week setFrame:CGRectMake(labelX, labelY, labelW, labelH)];
    [week setText:@""];
    [week setTextColor:[UIColor lightGrayColor]];
    [self setLabelType:week];
    [self addSubview:week];
    self.weekLabel = week;
    [week release];
    
    //电器
    labelY = labelY + labelH;
    UILabel *wiring = [[UILabel alloc]init];
    [wiring setFrame:CGRectMake(labelX, labelY, labelW, labelH)];
    [wiring setText:@""];
    [wiring setTextColor:[UIColor lightGrayColor]];
    [self setLabelType:wiring];
    [self addSubview:wiring];
    self.wiringLabel = wiring;
    [wiring release];
    
}

- (void)setLabelType:(UILabel *)label
{
    UIFont *font = [UIFont boldSystemFontOfSize:12];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setFont:font];
}
- (void)onOffAction
{
//    self.onOffBtn.selected = !self.onOffBtn.selected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(TimingSetCell:OnOff:)]) {
        [self.delegate TimingSetCell:self OnOff:self.onOffBtn.selected];
    }
}

- (void)settingAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(TimingSetCellSetting:)]) {
        [self.delegate TimingSetCellSetting:self];
    }
}

- (void)clearAction
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(TimingSetCellClear:)]) {
        [self.delegate TimingSetCellClear:self];
    }
}

@end
